import { createRouter, createWebHistory } from 'vue-router'
import Login from '../components/auth/Login.vue'
import Register from '../components/auth/Register.vue'
import PersonnelList from '../components/personnel/PersonnelList.vue'
import PersonnelForm from '../components/personnel/PersonnelForm.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/personnel',
    name: 'PersonnelList',
    component: PersonnelList,
    meta: { requiresAuth: true }
  },
  {
    path: '/personnel/add',
    name: 'AddPersonnel',
    component: PersonnelForm,
    meta: { requiresAuth: true }
  },
  {
    path: '/personnel/edit/:id',
    name: 'EditPersonnel',
    component: PersonnelForm,
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../components/user/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/change-password',
    name: 'ChangePassword',
    component: () => import('../components/user/ChangePassword.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.matched.some(record => record.meta.requiresAuth) && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router 